package com.apes.scm.income.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.finance.model.IncomeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = IncomeItem.CONST_TABLE_NAME)
public class IncomeItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "pur_income_item";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "收入明细")
    @Comment("明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "income_id", referencedColumnName = "id")
    @Comment("收入编码")
    private Income income; /*收入主表*/

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax; //税率

    @Transient
    private String incomeId;

    public String getIncomeId() {
        return income.getId();
    }


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("收入类型")
    private IncomeType incomeType;  //收入类型

    @Transient
    private String incomeTypeName;

    public String getIncomeTypeName() {
        return incomeType.getId();
    }

//    @Column(name = "business_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
//    @Comment("业务发生额")
//    private double businessAmount = 0; //业务发生额
//
//    @Column(name = "rate", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
//    @Comment("返利率")
//    private double rate = 0; //返利率

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("收入金额")
    private double incomeAmount = 0; //收入金额

    public double getFreezeAmount() {
        return freezeAmount;
    }

    public void setFreezeAmount(double freezeAmount) {
        this.freezeAmount = freezeAmount;
    }

    @Column(name = "freeze_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("冻结金额")
    private double freezeAmount = 0;

    public void changeFreezeAmount(double amount) {
         freezeAmount += amount;
         //取消判断，因执行不通过
         /*if(incomeAmount - recoveryAmount < freezeAmount && freezeAmount != 0) {
             throw new RuntimeException("冻结金额大于待收金额");
         }*/
    }


//    @Column(name = "adjustment_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
//    @Comment("调整金额")
//    private double adjustmentAmount = 0; //调整金额
//
//    @Column(name = "pending_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
//    @Comment("待收回金额")
//    private double pendingAmount = 0; //待收回金额
//
    @Column(name = "recovery_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已扣款金额")
    private double recoveryAmount = 0; //收回金额

    public void changeRecoveryAmount(double amount) {
        recoveryAmount += amount;
        if(recoveryAmount + freezeAmount > incomeAmount) {
            throw new RuntimeException("收款金额大于待收金额");
        }
    }


    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    @Comment("业务开始日期")
    private Date startDate; //业务开始日期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("业务结束日期")
    private Date endDate; //业务结束日期


    @Column(name = "done", nullable =false, columnDefinition = "number(1,0) default 0")
    @Comment("是否完成")
    private boolean done = false;

    public boolean isDone() {
        return done;
    }

    /*
    * 更新完成状态
    * */
    public void updateDone() {
        if(this.incomeAmount - this.freezeAmount - this.recoveryAmount == 0) {
            this.setDone(true);
        }else {
            this.setDone(false);
        }
    }

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

    /**
     * 附件
     */
    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "annex", columnDefinition = "CLOB")
    private String annex;

    @Column(name = "remark", length = 250)
    @Comment("来源明细明细ID")
    private String remark; //备注
}
